/**
 * Created by levy on 16/6/15.
 */
import Person from "./Person";

{
    const lf = liefeng,
        r = React;

    const DepartmentDetail = React.createClass({
        getInitialState: function () {
            return {
                departmentCon: ''
            }
        },
        componentWillMount: function () {
            var dt = this.props.data,
                aLinks = dt.staffList.map( function ( item ) {
                    return (
                        <Person
                            maxCount={this.props.maxCount}
                            item={item} />
                    )
                }.bind(this));
            this.setState({
                departmentCon: aLinks
            });
        },
        render: function () {
            var dt = this.props.data,
                departmentName = dt.deptname;
            if ( dt.staffList.length == 0 ) {
                return (
                    <div></div>
                )
            }
            return (
                <div className="department-item">
                    <h3>{departmentName}</h3>
                    {this.state.departmentCon}
                </div>
            )
        }
    });

    /**
     * addClass 额外的类. 默认不同的部门不在同一行.需要它们在同一行时, 传 no-department
     * @desc [人员选择] 默认 选择通讯录里的人。此时需要getUser
     * @args 
     *      method 默认是 lf.api.addressBook, 可自定义
     *      type 弃用。之前写得不好。现在直接传method就好了
     *      shouldUpdate: true 则componentWillReceiveProps时重新触发ajax请求
     */
    var SelectPerson = React.createClass({
        getInitialState: function () {
            return {
                holdSelectCon: [],
                currProps: '',
            }
        },
        getDefaultProps: function () {
            return {
                maxCount: 1
            }
        },
        componentWillMount: function () {
            this.getPersonList();
        },
        componentWillReceiveProps(nextProps) {
            //daily-work-add 传 userType
            if(nextProps.userType == this.state.currProps) return;
            else {
                this.setState({currProps: nextProps.userType});
                this.getPersonList();
            }

        },
        render: function () {
            return (
                <div ref="persons" className={"bottom-form-name department-con " +
					(this.props.selectClass + ' ' + this.props.addClass || '')} >
                    {this.state.holdSelectCon}
                </div>
            )
        },
        getPersonList() {
            var type = this.props.type || 'helper',
                currTask = this.props.currTask || {},
                user = this.props.user || {},
                method = this.props.method;

            if(!method) {
                method = lf.api.addressBook;
            }
            if(type == 'auditor'){
                method = lf.api.task.auditor;
            }
            else if(type == 'dealer'){
                method = lf.api.task.nextOperator;
            }

            //TODO 给个捕获机制
            //TODO 传个 ajaxData
            lf.utils.ajax({
                data: {
                    //通讯录只要这传这个
                    staffId: type == 'helper' ? liefeng.userInfo.id : user.id,

                    projectId: this.props.projectId || user.projectId , //后面用这个
                    eventId: currTask.eventId || this.props.eventId,
                    taskName: 'distributeLeaflets',

                    userType: this.props.userType, //获取日常任务办理人
                }
            }, method, function ( data ) {
                var dt = data,
                    holdSelectCon = dt.map( function ( item ) {
                        return (
                            <DepartmentDetail maxCount={this.props.maxCount} data={item} />
                        )
                    }.bind(this));
                this.setState({
                    holdSelectCon: holdSelectCon
                })
            }.bind(this));
        }
    });
}

export default SelectPerson;
